package cn.rubintry.bug.hunting.activity

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import cn.rubintry.bug.common.base.BaseActivity
import cn.rubintry.bug.hunting.R
import cn.rubintry.bug.hunting.databinding.ActivityLogDetailBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File

class LogDetailActivity : BaseActivity<ActivityLogDetailBinding>() {

    /**
     * 日志文件
     */
    private var file : File ?= null

    override fun bindingClazz(): Class<ActivityLogDetailBinding> {
        return ActivityLogDetailBinding::class.java
    }

    override fun getActivityParams() {
        val path = intent.getStringExtra("filePath") ?: ""
        file = File(path)
    }

    override fun initView() {
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    override fun observeData() {
        CoroutineScope(Dispatchers.IO).launch {
            val content = file?.readText() ?: ""
            CoroutineScope(Dispatchers.Main).launch {
                binding?.tvContent?.text = content
            }
        }
    }

    override fun onNetWorkError(msg: String?) {

    }

}